﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;


/// <summary>
/// SQ_Eval 的摘要说明
/// </summary>
public class SQ_Eval
{
    DBHelper db = new DBHelper();
    private DateTime NowKq;
    private string kq_s_d;
    private string kq_e_d;

    private int kp_year = 0;
    private int kp_month = 0;
    public int now_year = 0;
    public int now_month = 0;
    public int last_year = 0;
    public int last_month = 0;
    private string now_kq_s_d;
    private string now_kq_e_d;

    public SQ_Eval()
    {
        string sql = "select * from A_Eval_Sys where [state]=0";
        DataTable dt = db.getList(sql);
        int year = Convert.ToInt32(dt.Rows[0]["year"].ToString());
        kp_year = year;
        int month = Convert.ToInt32(dt.Rows[0]["month"].ToString());
        kp_month = month;

        NowKq = Convert.ToDateTime(year + "-" + month + "-01");
        kq_s_d = dt.Rows[0]["rq_start"].ToString();
        kq_e_d = dt.Rows[0]["rq_end"].ToString();


        DateTime now = DateTime.Now;
        int day = now.Day;
        if (day >= 26)
        {
            now_year = now.AddMonths(1).Year;
            now_month = now.AddMonths(1).Month;
            now_kq_s_d = now.Year.ToString() + "-" + now.Month.ToString() + "-26";
            now_kq_e_d = now.AddMonths(1).Year.ToString() + "-" + now.AddMonths(1).Month.ToString() + "-25";
            last_year = now.Year;
            last_month = now.Month;
        }
        else
        {
            now_year = now.Year;
            now_month = now.Month;
            now_kq_s_d = now.AddMonths(-1).Year.ToString() + "-" + now.AddMonths(-1).Month.ToString() + "-26";
            now_kq_e_d = now.Year.ToString() + "-" + now.Month.ToString() + "-25";
            last_year = now.AddMonths(-1).Year;
            last_month = now.AddMonths(-1).Month;
        }

    }


    public void StartEval(string sign)
    {
        //计算完毕之后记得写入统计
        switch (sign)
        {
            case "0":
                SetEvalMain();
                SetGdTask();
                SetZdTask();
                SetTaskEvalGp();
                SetEvalGpF();
                SetEvalGp();
                SetSpecial();
                string sql = "insert into A_Eval_Log (type,logtxt) values('0',N'自动计算考评')";
                db.executeNoneQuery(sql);
                break;
            case "1":
                SetEvalMain();
                break;
            case "2":
                SetGdTask();
                break;
            case "3":
                SetZdTask();
                break;
            case "4":
                SetTaskEvalGp();
                SetEvalGpF();
                SetEvalGp();
                break;
            case "5":
                SetSpecial();
                break;

        }
    }

    #region 自动生成当月考评总表及考评表 

    /// <summary>
    /// (一)自动生成当月考评总表及考评表
    /// </summary>
    private void SetEvalMain()
    {
        DateTime start_day = Convert.ToDateTime(now_kq_s_d);
        DateTime end_day = Convert.ToDateTime(now_kq_e_d);
        string sql = "";
        sql = "select * from dict_dept_position where evalid>0 and  isUsing=1 and ((LastYear<" + now_year + ") or(LastYear=" + now_year + " and  LastMonth<" + now_month + "))";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            //这里需要调用两个方法 写入总表 写入考评总表 
            string Psid = dr["id"].ToString();
            string UserBh = dr["userbh"].ToString();
            string Evalid = dr["evalid"].ToString();
            int Year = now_year;
            int Month = now_month;
            int Relid = WriteEvalMain(Psid, UserBh, Year, Month);
            WriteEvalDetail(Relid, Evalid, Psid, Year, Month);
            ii++;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(1,N'成功写入" + now_year + "年" + now_month + "月统计记录" + ii + "条')";
        db.executeNoneQuery(sql);

    }

    /// <summary>
    /// 写入总表
    /// </summary>
    /// <param name="Psid">岗位ID</param>
    /// <param name="UserBh">当前在职人编号</param>
    /// <param name="Year">考评年</param>
    /// <param name="Month">考评月</param>
    /// <returns></returns>
    private int WriteEvalMain(string Psid, string UserBh, int Year, int Month)
    {
        string sql = "insert into A_Eval_Main (psid,userbh,year,month) values ('" + Psid + "','" + UserBh + "','" + Year + "','" + Month + "')";
        int Relid = db.executeNoneQueryAndReturnLatestValue(sql);
        return Relid;
    }

    /// <summary>
    /// 写入考评明细表
    /// </summary>
    /// <param name="Relid">考评总表关联ID</param>
    /// <param name="Evalid">当前岗位模板编号</param>
    /// <param name="Psid">岗位ID</param>
    /// <param name="Year">考评年</param>
    /// <param name="Month">考评月</param>
    private void WriteEvalDetail(int Relid, string Evalid, string Psid, int Year, int Month)
    {
        string sql = "select * from dict_dept_position_evaluate_detail where IsUsing=1 and relid=" + Evalid;
        DataTable dt = db.getList(sql);
        foreach (DataRow dr in dt.Rows)
        {
            string id = dr["id"].ToString();
            string qz = dr["qz"].ToString();

            sql = "insert into A_Eval_Main_Detail(Relid,psid,evelid,eveldeid,year,month,evalSp,evalGp) values ('" + Relid + "','" + Psid + "','" + Evalid + "','" + id + "','" + Year + "','" + Month + "','" + qz + "',0)";
            db.executeNoneQuery(sql);

        }
        sql = "update [dict_dept_position] set LastYear='" + Year + "',LastMonth='" + Month + "'  where evalid>0 and  isUsing=1";
        db.executeNoneQuery(sql);
    }
    #endregion

    #region 自动生成当月固定任务

    /// <summary>
    /// 自动生成当月固定任务
    /// </summary>
    private void SetGdTask()
    {
        DateTime start_day = Convert.ToDateTime(now_kq_s_d);

        string sql = "";
        sql = "select * from dict_dept_position_Task where isUsing=1 and ((Year<" + now_year + ") or(Year=" + now_year + " and  Month<" + now_month + "))";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            int sc = Convert.ToInt16(dr["sc_d"].ToString());
            DateTime end_day = start_day.AddDays(sc);
            string id = dr["id"].ToString();
            sql = "insert into A_Task(type,title,detail,rq_start,rq_yjs,cz_userbh,fb_username,zx_psid,ys_psid,eval_ps,eval_parps,eval_deptps,Year,Month,state,ps_par,ps_dept) values(";
            sql += "0,N'" + dr["title"].ToString() + "',N'" + dr["detail"].ToString() + "',N'" + start_day.ToString("yyyy-MM-dd") + "',N'" + end_day.ToString("yyyy-MM-dd") + "','00000001',N'系统发布','" + dr["zx_psid"].ToString() + "','" + dr["ys_psid"].ToString() + "','" + dr["eval_ps"].ToString() + "','" + dr["eval_parps"].ToString() + "','" + dr["eval_deptps"].ToString() + "','" + now_year + "','" + now_month + "',2,'" + dr["ps_par"].ToString() + "','" + dr["ps_dept"].ToString() + "')";
            int i = db.executeNoneQuery(sql);
            if (i == 1)
            {
                sql = "update dict_dept_position_Task set Year=" + now_year + ",Month=" + now_month + " where id=" + id;
                db.executeNoneQuery(sql);
                ii++;
            }

        }

        sql = "insert into A_Eval_Log(type,logtxt) values(2,N'成功写入" + now_year + "年" + now_month + "月固定任务" + ii + "条')";
        db.executeNoneQuery(sql);

    }
    #endregion

    #region 计算自动完成任务

    /// <summary>
    /// 计算自动完成任务
    /// </summary>
    private void SetZdTask()
    {
        string now_day = DateTime.Now.ToString();

        string sql = "";
        sql = "select * from dict_dept_position where isUsing=1 and iszdwc=1";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            int zx_psid = Convert.ToInt16(dr["id"].ToString());

            sql = "update A_Task set [state]=3,rq_sjs=getdate() where zx_psid=" + zx_psid + " and [state] not in(0,4) and rq_yjs<=getdate()";
            int i = db.executeNoneQuery(sql);
            ii = ii + i;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(3,N'成功写入" + now_year + "年" + now_month + "月自动完成任务" + ii + "条')";
        db.executeNoneQuery(sql);

        sql = "update A_Task set [state]=3,rq_sjs=getdate(),bz_js=N'系统自动完成任务' where [state] not in(0,4) and rq_yjs<=getdate() and [type]=0";
        db.executeNoneQuery(sql);

        if (DateTime.Now.Day >= 27)
        {
            sql = "update A_Task set [state]=4,GP=100,bz_ys=N'系统自动验收' where [state]=3 and [type]=0";
            db.executeNoneQuery(sql);
        }

    }
    #endregion

    #region 计算当月考评任务得分
    /// <summary>
    /// 计算当月考评任务得分
    /// </summary>
    private void SetTaskEvalGp()
    {
        string sql = "select * from A_Eval_Main where year=" + kp_year + " and month=" + kp_month;
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            string userbh = dr["userbh"].ToString();
            string psid = dr["psid"].ToString();

            sql = "select a.type,a.eval_ps,sum(a.sl) as sl,sum(a.Sp) as Sp,sum(a.Gp) as Gp from ";
            sql += " (";
            sql += "select [type], eval_ps as eval_ps, count(id) as sl, sum(Sp) as Sp, sum(Gp) as Gp from A_Task  where Year = " + kp_year + " and Month = " + kp_month + " and zx_psid = '" + psid + "' and state in (2,3,4) group   by zx_psid,[type], eval_ps";
            sql += " union all";
            sql += " select [type], eval_parps as eval_ps, count(id) as sl, sum(Sp) as Sp, sum(Gp) as Gp from A_Task  where Year = " + kp_year + " and Month = " + kp_month + " and ps_par = '" + psid + "' and state in (2,3,4) and eval_parps <> 0 group by ps_par,[type], eval_parps";
            sql += " union all";
            sql += " select [type], eval_deptps as eval_ps, count(id) as sl, sum(Sp) as Sp, sum(Gp) as Gp from A_Task  where Year = " + kp_year + " and Month = " + kp_month + " and ps_dept = '" + psid + "' and state in (2,3,4)  and eval_deptps <> 0 group by ps_dept,[type], eval_deptps";
            sql += " ) a group by a.type,a.eval_ps";
            DataTable dt_T = db.getList(sql);
            foreach (DataRow dr_e in dt_T.Rows)
            {
                string type = dr_e["type"].ToString();
                string ps = dr_e["eval_ps"].ToString();
                string sl = dr_e["sl"].ToString();
                string Sp = dr_e["Sp"].ToString();
                string Gp = dr_e["Gp"].ToString();
                switch (type)
                {
                    case "0":
                        sql = "update A_Eval_Main_Detail set taskGn=" + sl + ",taskGSp=" + Sp + ",taskGGp=" + Gp + " where psid=" + psid + " and eveldeid='" + ps + "' and year=" + kp_year + " and month=" + kp_month;
                        break;
                    case "1":
                        sql = "update A_Eval_Main_Detail set taskLn=" + sl + ",taskLSp=" + Sp + ",taskLGp=" + Gp + " where psid=" + psid + " and eveldeid='" + ps + "' and year=" + kp_year + " and month=" + kp_month;
                        break;
                }
                ii++;
                db.executeNoneQuery(sql);
            }

        }
        sql = "insert into A_Eval_Log(type,logtxt) values(4,N'成功计算" + kp_year + "年" + kp_month + "月任务得分" + ii + "条')";
        db.executeNoneQuery(sql);
    }
    #endregion

    #region 考评分表
    /// <summary>
    /// 计算考评分表
    /// </summary>
    private void SetEvalGpF()
    {
        string sql = "select * from A_Eval_Main_Detail where year=" + kp_year + " and month=" + kp_month;
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            string id = dr["id"].ToString();
            double taskGGp = Convert.ToDouble(dr["taskGGp"].ToString());
            double taskGSp = Convert.ToDouble(dr["taskGSp"].ToString());

            double taskLGp = Convert.ToDouble(dr["taskLGp"].ToString());
            double taskLSp = Convert.ToDouble(dr["taskLSp"].ToString());

            double evalSp = Convert.ToDouble(dr["evalSp"].ToString());
            double evalGp = 0;

            double taskGp = taskGGp + taskLGp;
            double taskSp = taskGSp + taskLSp;
            if (taskGSp > taskGGp)
            {
                evalGp = 0;
            }
            else
            {
                if (taskSp == 0)
                {
                    evalGp = evalSp;
                }
                else if (taskSp > 0)
                {
                    evalGp = (taskGp / taskSp) * evalSp;
                }
            }
            evalGp = Math.Round(evalGp, 2);
            sql = "update A_Eval_Main_Detail set evalGp='" + evalGp + "' where id=" + id;
            db.executeNoneQuery(sql);
            ii++;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(4,N'成功计算" + kp_year + "年" + kp_month + "月考评分表" + ii + "条')";
        db.executeNoneQuery(sql);
    }
    #endregion

    #region 考评总表
    /// <summary>
    /// 计算考评总表
    /// </summary>
    private void SetEvalGp()
    {
        string sql = "select a.* from ( ";
        sql += " select 'G' as sign,psid , sum(taskGn) as sl,sum(taskGSp) as Sp,sum(taskGGp) as Gp from A_Eval_Main_Detail where  [year] = " + kp_year + " and[month] = " + kp_month;
        sql += " group by psid ";
        sql += " union all ";
        sql += " select 'L' as sign,psid , sum(taskLn) as sl,sum(taskLSp) as Sp,sum(taskLGp) as Gp from A_Eval_Main_Detail where  [year] = " + kp_year + "  and[month] = " + kp_month;
        sql += " group by psid ";
        sql += " union all ";
        sql += " select 'E' as sign,psid ,count(id) as sl,sum(evalSp) as Sp, sum(evalGp) as Gp from A_Eval_Main_Detail where  [year] = " + kp_year + "  and[month] = " + kp_month;
        sql += " group by psid ) a order by a.psid,a.sign ";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            string sign = dr["sign"].ToString();
            string psid = dr["psid"].ToString();
            string sl = dr["sl"].ToString();
            string Sp = dr["Sp"].ToString();
            string Gp = dr["Gp"].ToString();

            switch (sign)
            {
                case "E":
                    sql = "update A_Eval_Main set evalSp=" + Sp + ",evalGp=" + Gp + " where psid='" + psid + "' and year=" + kp_year + " and month=" + kp_month;
                    break;
                case "G":
                    sql = "update A_Eval_Main set taskGn=" + sl + ",taskGSp=" + Sp + ",taskGGp=" + Gp + " where psid='" + psid + "' and year=" + kp_year + " and month=" + kp_month;
                    break;
                case "L":
                    sql = "update A_Eval_Main set taskLn=" + sl + ",taskLSp=" + Sp + ",taskLGp=" + Gp + " where psid='" + psid + "' and year=" + kp_year + " and month=" + kp_month;
                    break;
            }
            db.executeNoneQuery(sql);
            ii++;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(4,N'成功计算" + kp_year + "年" + kp_month + "月考评总表" + ii + "条')";
        db.executeNoneQuery(sql);
    }
    #endregion

    #region 特殊扣分
    /// <summary>
    /// 计算特殊扣分
    /// </summary>
    private void SetSpecial()
    {
        string sql = "select sum(fz) as fz,psid from A_Special_Eval where  isUsing=1 and [year]= " + kp_year + " and [month] = " + kp_month + " group by psid";
        DataTable dt = db.getList(sql);
        int ii = 0;
        foreach (DataRow dr in dt.Rows)
        {
            string psid = dr["psid"].ToString();
            string fz = dr["fz"].ToString();
            sql = "update A_Eval_Main set special=" + fz + ",evalGp=evalGp-" + fz + " where psid='" + psid + "' and year=" + kp_year + " and month=" + kp_month;
            db.executeNoneQuery(sql);
            ii++;
        }
        sql = "insert into A_Eval_Log(type,logtxt) values(5,N'成功计算" + kp_year + "年" + kp_month + "月特殊考评" + ii + "条')";
        db.executeNoneQuery(sql);
    }
    #endregion

}
